package com.googlecode.gwtbb.client.only.glue.events;

import com.googlecode.gwtbb.client.only.glue.events.forum.ForumEventListener;
import com.googlecode.gwtbb.client.only.glue.events.forum.PostEventListener;
import com.googlecode.gwtbb.client.only.glue.events.forum.PostsEventListener;
import com.googlecode.gwtbb.client.only.glue.events.user.UserAuthenticationEventListener;
import com.googlecode.gwtbb.client.only.glue.events.user.UserEventListener;
import com.googlecode.gwtbb.client.shared.remote.forum.CreatePostEvent;
import com.googlecode.gwtbb.client.shared.remote.forum.ForumEvent;
import com.googlecode.gwtbb.client.shared.remote.forum.PostsEvent;
import com.googlecode.gwtbb.client.shared.remote.user.UserEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 20, 2008
 */
public class GwtbbEvents {
	private static final GwtbbEvents instance = new GwtbbEvents();
	List<ForumEventListener> forumEventListeners = new ArrayList<ForumEventListener>();
	List<PostEventListener> postEventListeners = new ArrayList<PostEventListener>();
	List<PostsEventListener> postsEventListeners = new ArrayList<PostsEventListener>();
	List<UserAuthenticationEventListener> userAuthenticationEventListeners = new ArrayList<UserAuthenticationEventListener>();
	List<UserEventListener> userEventListeners = new ArrayList<UserEventListener>();

	/**
	 * @return the singleton instance of this class
	 */
	public static GwtbbEvents instance() {
		return instance;
	}

	/**
	 * @see GwtbbEvents#instance()
	 */
	private GwtbbEvents() {
	}

	public void addForumEventListener(ForumEventListener listener) {
		forumEventListeners.add(listener);
	}

	public void addPostEventListener(PostEventListener listener) {
		postEventListeners.add(listener);
	}

	public void addPostsEventListener(PostsEventListener listener) {
		postsEventListeners.add(listener);
	}

	public void addUserAuthenticationEventListener(UserAuthenticationEventListener listener) {
		userAuthenticationEventListeners.add(listener);
	}

	public void addUserEventListener(UserEventListener listener) {
		userEventListeners.add(listener);
	}

	public void fireCreateForumEvent(ForumEvent event) {
		for (ForumEventListener listener : forumEventListeners)
			listener.onCreate(event);
	}

	public void fireCreatePostEvent(CreatePostEvent event) {
		for (PostEventListener listener : postEventListeners)
			listener.onCreate(event);
	}

	public void fireCreateUserEvent(UserEvent event) {
		for (UserEventListener listener : userEventListeners)
			listener.onCreate(event);
	}

	public void fireLoginUserEvent(UserEvent event) {
		for (UserAuthenticationEventListener listener : userAuthenticationEventListeners)
			listener.onLogin(event);
	}

	public void fireLogoutUserEvent(UserEvent event) {
		for (UserAuthenticationEventListener listener : userAuthenticationEventListeners)
			listener.onLogout(event);
	}

	public void fireSearchPostsEvent(PostsEvent event) {
		for (PostsEventListener listener : postsEventListeners)
			listener.onSearch(event);
	}
}
